import main
import json,datetime


test_json = '{"coord":{"lon":30.2642,"lat":59.8944},"weather":[{"id":802,"main":"Clouds","description":"scattered clouds","icon":"03d"}],"base":"stations","main":{"temp":18.92,"feels_like":18.67,"temp_min":18.92,"temp_max":20.08,"pressure":1001,"humidity":69},"visibility":10000,"wind":{"speed":5,"deg":310},"clouds":{"all":40},"dt":1690128543,"sys":{"type":2,"id":197864,"country":"RU","sunrise":1690075134,"sunset":1690138295},"timezone":10800,"id":498817,"name":"Saint Petersburg","cod":200}'
test_r = json.loads(test_json)

def test_get_weather_attr():
    """Test how works func get_weather_attr"""
    res = main.get_weather_attr(test_r)

    assert res.city_name == "Санкт-Петербург","Problem with name in func get_weather_attr"
    assert res.city_temp == 18.92,"Problem with temp in func get_weather_attr"
    assert res.city_humidity == 69,"Problem with humidity in func get_weather_attr"
    assert res.wind == 5,"Problem with wind in func get_weather_attr"
    assert res.sunrise == datetime.datetime(year=2023,month=7,day=23,hour=4,minute=18,
                                            second=54),"Problem with sunrise in func get_weather_attr"
    assert res.sunset == datetime.datetime(year=2023, month=7, day=23, hour=21, minute=51,
                                            second=35), "Problem with sunset in func get_weather_attr"
    assert res.day_length == datetime.timedelta(seconds=63161), "Problem with day_length in func get_weather_attr"
    assert res.weather_description == "Облачно","Problem with weather_description in func get_weather_attr"

test_city_weather = main.City_Weather(city_name="Санкт-Петербург",
                                 city_temp=18.92,
                                 city_humidity=69,
                                 city_pressure=1001,
                                 wind=5,
                                 sunrise=datetime.datetime(year=2023,month=7,day=23,hour=4,minute=18,second=54),
                                 sunset=datetime.datetime(year=2023, month=7, day=23, hour=21, minute=51,second=35),
                                 day_length=datetime.timedelta(seconds=63161),
                                 weather_description="Облачно")

def test_print_weather():
    """Test how works func print_weather"""
    res = main.print_weather(test_city_weather)
    assert res == f'В городе Санкт-Петербург \n'\
           'Температура воздуха составит: 18.92 C° \n'\
          f'Влажность воздуха: 69%\n'\
          f'Давление: 1001 мм.р.т.\n'\
          f'Ветер: 5 м/с\n'\
          f'Восход: 2023-07-23 04:18:54\n'\
          f'Закат: 2023-07-23 21:51:35\n'\
          f'Продолжительность дня: 17:32:41\n'\
          f'На улице: Облачно'

def test_global():
    """Test availability of service and values of temp and winds speed"""
    r = main.get_weather("Moscow")
    city_weather = main.get_weather_attr(r)
    assert -50 < city_weather.city_temp < 50,"Возвращена неадекватная температура"
    assert 0 < city_weather.wind < 30,"Возвращена неадекватный ветер"